package com.vooda.business_admin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vooda.business_admin.common.ReqDataTableDto;
import com.vooda.business_admin.common.RspDataTableDto;
import com.vooda.business_admin.config.CdnProperties;
import com.vooda.business_admin.dao.StoreMapper;
import com.vooda.business_admin.entity.Store;
import com.vooda.business_admin.service.StoreService;
import com.vooda.business_admin.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(readOnly = true)
public class StoreServiceImpl implements StoreService{

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private CdnProperties cdnProperties;

    @Override
    public List<Store> getStoreList() {
        return storeMapper.getStoreList();
    }

    @Override
    @Transactional(readOnly = false)
    public String deleteByPrimaryKey(Long id) {
        Store store = new Store();
        store.setId(id);
        store.setIsDel(true);
        return updateByPrimaryKeySelective(store);
    }

    @Override
    @Transactional(readOnly = false)
    public String insertSelective(Store record) {
        Long maxId = storeMapper.findMaxId();
        record.setId(maxId+1);
        record.setCreateTime(new Date());
        record.setIsDel(false);
        record.setModifiedTime(new Date());
        return storeMapper.insertSelective(record)>0?"success":"fail";
    }

    @Override
    public Store selectByPrimaryKey(Long id) {
        return storeMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public String updateByPrimaryKeySelective(Store record) {
        record.setModifiedTime(new Date());
        return storeMapper.updateByPrimaryKeySelective(record)>0?"success":"fail";
    }

    @Override
    public RspDataTableDto<Store> findAllStoreList(ReqDataTableDto dataTableDTO, String marketId) {

        PageHelper.startPage(dataTableDTO.getPage()+1,dataTableDTO.getLength());
        List<Store> list = storeMapper.findAllStoreList(StringUtils.isBlank(marketId)?null:Long.parseLong(marketId));
        PageInfo<Store> pageInfo =  new PageInfo<Store>(list);
        return new RspDataTableDto<Store>(dataTableDTO.getDraw(),pageInfo.getTotal(),pageInfo.getList());

    }

    @Override
    @Transactional(readOnly = false)
    public String saveStore(Store store,MultipartFile image) {
        if (!ObjectUtils.isEmpty(image)) {
            //上传封面图片
            MultipartFile file = image;
            File f = new File(cdnProperties.getBasePath());
            if (!f.exists()) {
                f.mkdirs();
            }
            String oldName = file.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));

            File newFile = new File(f.getAbsolutePath(), newName);
            try {
                newFile.createNewFile();
                file.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            store.setImageUrl(newName);
        }

        return store.getId() != null?updateByPrimaryKeySelective(store):insertSelective(store);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateImage(MultipartFile imageUpload, Long id) {
        File f = new File(cdnProperties.getBasePath());
        if (!f.exists()) {
            f.mkdirs();
        }
        String oldName = imageUpload.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        File newFile = new File(f.getAbsolutePath(), newName);

        try {
            newFile.createNewFile();
            imageUpload.transferTo(newFile);
            imageUpload.getInputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Store store = selectByPrimaryKey(id);
        store.setImageUrl(newName);
        store.setModifiedTime(new Date());
        updateByPrimaryKeySelective(store);
    }

}
